package com.inspur.edp.common.expr.visitor.field;

import com.inspur.edp.common.expr.constant.ExpressConstant;
import com.inspur.edp.common.expr.exception.ExpressException;
import com.inspur.edp.common.expr.spi.FieldDeserializeVisitor;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FieldDeserializeFactory {

    private final static Object lockObject = new Object();

    private static Map<String, FieldDeserializeVisitor> PROVIDER_MAP;

    public static FieldDeserializeVisitor getSpi(String type) {
        if (PROVIDER_MAP == null) {
            synchronized (lockObject) {
                if (PROVIDER_MAP == null) {
                    ConcurrentHashMap<String, FieldDeserializeVisitor> temp = new ConcurrentHashMap<>();

                    //固定的几个
                    temp.put(ExpressConstant.JSON_STRING_TO_DATE, new DateSerializeVisitor());

                    temp.put(ExpressConstant.JSON_STRING_TO_DATE_TIME, new DateTimeSerializeVisitor());

                    PROVIDER_MAP = temp;
                }
            }
        }
        FieldDeserializeVisitor genSpi = PROVIDER_MAP.get(type);
        if (genSpi != null) {
            return genSpi;
        } else {
            throw new ExpressException("not find field deserialize provider " + type);
        }
    }

}
